﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using DataBaseConnection;
using System.Collections.Generic;

namespace DataEntity
{
    /// <summary>
    /// 分类结构
    /// </summary>
    public partial class tab_Class : IEntity
    {
        /// <summary>
        /// 选择指定的栏目节点集合
        /// </summary>        
        public static DataTable SelectListForLocation(string _rootPath, string _endPath)
        {
            //获取层级节点集合
            List<string> structPaths = new List<string>();
            structPaths.Add(_endPath);
            int sLength = _rootPath.Length; //起始路径长度
            string subpath = _endPath;   //每次循环截取的字符串

            int clen = 0;
            while (subpath.Length > sLength)
            {
                //分析截取长度
                clen = subpath.Length - 2;
                subpath = subpath.Substring(0, clen);
                structPaths.Add(subpath);
            }

            string ucols = "(cls_StructId + cls_NodeValue)";
            string myWhere = DataEntity.Common.UniteCondition(structPaths.ToArray(), ucols, true);

            return DataEntity.Common.ExecuteDataSet("SELECT * FROM tab_Class WHERE " + myWhere + " ORDER BY " + ucols, null, null).Tables[0];
        }

        /// <summary>
        /// 选择一个栏目节点集合
        /// </summary>        
        public static DataTable SelectList(string FullPath, int _level, bool showTop)
        {
            string ucols = "(cls_StructId + cls_NodeValue)";
            string structPath = (showTop) ? ucols : "cls_StructId";
            string sql = "SELECT * FROM tab_Class WHERE " + structPath + " LIKE @FullPath + '%'";

            //层级
            if (_level != -1)
            {
                sql += " AND (LEN" + ucols + " - LEN(@FullPath))/2 <= " + _level;
            }
            //排序
            sql += " ORDER BY " + ucols;
            return DataEntity.Common.ExecuteDataSet(sql, new string[] { "@FullPath" }, new object[] { FullPath }).Tables[0];
        }

        /// <summary>
        /// 返回当前栏目节点的最大序列值
        /// </summary>
        /// <returns>01</returns>
        public static string GetMaxValue(string StructId)
        {
            int output = DataEntity.Common.ExecuteScalar<int>("select top 1 cls_NodeValue from tab_Class where cls_StructId = @cls_StructId ORDER BY cls_NodeValue DESC",
                new string[] { "@cls_StructId" }, new object[] { StructId });

            output += 1;

            return string.Format("{0:d2}", output);
        }

        /// <summary>
        /// 改变子节点的父结构
        /// </summary>        
        public static int UpdateSub(string OldPath, string NewPath)
        {
            string sql = @"UPDATE tab_Class SET cls_StructId = @NewPath + right(cls_StructId, len(cls_StructId) - len(@OldPath))
where cls_StructId like @OldPath + '%'";
            return DataEntity.Common.ExecuteNonQuery(sql, new string[] { "@NewPath", "@OldPath" }, new object[] { NewPath, OldPath });
        }

        /// <summary>
        /// 剪切栏目节点
        /// </summary>        
        public static int Cut(string OldNode, string TargetNode)
        {
            //修改顶部节点
            string maxvalue = GetMaxValue(TargetNode);  //该节点的最大子节点值
            string sql = "UPDATE tab_Class SET cls_StructId = @cls_StructId, cls_NodeValue =  = @MaxValue WHERE (cls_StructId + cls_NodeValue) = @FullPath";

            int result = DataEntity.Common.ExecuteNonQuery(sql,
                new string[] { "@cls_StructId", "@MaxValue", "@FullPath" },
                new object[] { TargetNode, maxvalue, OldNode });

            //修改子节点
            result += UpdateSub(OldNode, TargetNode + maxvalue);
            return result;
        }

        /// <summary>
        /// 复制栏目节点
        /// </summary>        
        public static int CopyTo(string OldNode, string TargetNode)
        {
            //获取一个节点的数据
            DataTable dt = SelectList(OldNode, -1, true);
            if (dt == null || dt.Rows.Count == 0) { return 0; }

            string maxval = GetMaxValue(TargetNode);
            int result = 0;//影响行数
            DataEntity.tab_Class t = new tab_Class();

            //循环加载插入语句
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];

                t.SetValues(dr);
                t.cls_PubDate = DateTime.Now;

                //是否第一个节点，不同点在于“结构编号”和“节点序列”
                if (i == 0)
                {
                    t.cls_StructId = TargetNode;
                    t.cls_NodeValue = maxval;
                }
                else
                {
                    t.cls_StructId = TargetNode + maxval + t.cls_StructId.Substring(OldNode.Length);
                }

                result += t.Insert();
            }

            return result;
        }

        /// <summary>
        /// 合并两个栏目节点
        /// </summary>        
        public static int Unite(string OldNode, string TargetNode)
        {
            //获取一个节点的数据
            DataTable dt = SelectList(OldNode, 1, false);
            if (dt == null || dt.Rows.Count == 0) { return 0; }

            //源节点一层
            DataEntity.tab_Class t = new tab_Class();
            int result = t.Delete("(cls_StructId + cls_NodeValue) = @FullPath", new string[] { "@FullPath" }, new object[] { OldNode });

            //将源节点子节点剪切
            //循环移动二级节点
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                t.SetValues(dr);
                string t_structpath = t.cls_StructId + t.cls_NodeValue;

                result += Cut(t_structpath, TargetNode);
            }

            return result;
        }
    }
}

